package com.amumu.drama.common.modules.order.mapping;

import cn.hutool.core.collection.CollUtil;
import com.amumu.drama.common.base.mapstruct.BaseMapping;
import com.amumu.drama.common.base.mapstruct.ext.LocalDateTimeToStringMapping;
import com.amumu.drama.common.modules.member.cache.MemberCache;
import com.amumu.drama.common.modules.order.dto.resp.MemberOrderListResp;
import com.amumu.drama.model.OmsOrder;
import com.amumu.drama.model.UmsMember;
import com.beust.jcommander.internal.Lists;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
 * @author sunzhuang
 * @date 2024/8/23 17:37
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo
 */
@Mapper(componentModel = "spring")
public interface MemberOrderListRespMapping extends BaseMapping<OmsOrder, MemberOrderListResp>, LocalDateTimeToStringMapping {
    MemberOrderListRespMapping INSTANCE = Mappers.getMapper(MemberOrderListRespMapping.class);

    /**
     * 正向映射的后置处理，List映射会自动继承此配置
     *
     * @param src
     * @param dest
     */
    @Override
    default void afterConvertTo(OmsOrder src, MemberOrderListResp dest) {
        if (CollUtil.isEmpty(dest.getOrderItemList())) {
            dest.setOrderItemList(Lists.newArrayList());
        }
        if (src.getMemberId()!=null){
            UmsMember member = MemberCache.INSTANCE.getFromCache(src.getMemberId());
            if (member!=null){
                dest.setMemberUsername(member.getUsername());
                dest.setMemberNickname(member.getNickname());
            }
        }
    }
}
